﻿using System.IO;
using System.Net;
using System.Text;
using KingTools.CommonsHelper.Constants;
using KingTools.SocketHelper.Model;
using KingTools.Utils;

namespace KingTools.SocketHelper
{
    /// <summary>
    ///     发送验证字符串
    /// </summary>
    public class SendImpl : ISend
    {
        public byte[] SendVerifyProtocol()
        {
            byte[] bytes = Encoding.Default.GetBytes("tgw_l7_forward\r\nHost:127.0.0.1:8002\r\n\r\n");
            return bytes;
        }

        public byte[] SendLoginProtocol(ProtocolLoginVO login)
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);

            writer.Write(login.baseVO.dataSize);
            writer.Write(IPAddress.HostToNetworkOrder(login.baseVO.protocolId));
            writer.Write(IPAddress.HostToNetworkOrder(login.baseVO.recNum));
            ByteBuffer.putString(stream, login.gsession);
            ByteBuffer.putString(stream, login.openId);
            ByteBuffer.putString(stream, login.openKey);
            ByteBuffer.putString(stream, login.pf);
            ByteBuffer.putString(stream, login.pfKey);
            writer.Write(IPAddress.HostToNetworkOrder(login.oprId));
            writer.Write(login.first);
            ByteBuffer.putString(stream, login.xy);
            writer.Write(IPAddress.HostToNetworkOrder(login.recommend));
            ByteBuffer.putString(stream, login.nickname);

            stream.Position = 0;
            login.baseVO.dataSize = IPAddress.HostToNetworkOrder((int) (stream.Length));
            writer.Write(login.baseVO.dataSize);

            writer.Flush();

            stream.Seek(0, SeekOrigin.Begin);

            var bytes = new byte[stream.Length];
            stream.Read(bytes, 0, (int) stream.Length);

            return bytes;
        }

        public byte[] SendBusinessProtocol(ProtocolBusinessVO business)
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream);

            var encryStream = new MemoryStream();
            var encryWriter = new BinaryWriter(encryStream);

            writer.Write(IPAddress.HostToNetworkOrder(business.baseVO.dataSize));
            encryWriter.Write(IPAddress.HostToNetworkOrder(business.baseVO.protocolId));
            encryWriter.Write(IPAddress.HostToNetworkOrder(business.baseVO.recNum));

            business.GetBytes(encryStream);
            encryWriter.Flush();
            encryStream.Position = 0;
            encryStream.Seek(0, SeekOrigin.Begin);
            var encryBytes = new byte[encryStream.Length];
            encryStream.Read(encryBytes, 0, (int) encryStream.Length);

            writer.Write(EncryUtil.encrypt(encryBytes, Constants.encryptKey));

            stream.Position = 0;
            business.baseVO.dataSize = IPAddress.HostToNetworkOrder((int) (stream.Length));
            writer.Write(business.baseVO.dataSize);

            writer.Flush();

            stream.Seek(0, SeekOrigin.Begin);
            var bytes = new byte[stream.Length];
            stream.Read(bytes, 0, (int) stream.Length);

            return bytes;
        }
    }
}